#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//1602连接端口
int readhigh = A0;//默认高电平读入端口
int readlow = A1;//默认低电平读入端口
int inputpin = 8;//按钮状态读入端口
long lastbuttonstate =  LOW;//定义上次开关状态
long lastbuttontime = 0;//定义一个时间
long debouncetime = 50;//定义防抖动时间
long buttonstate ;//定义开关的现在的状态
int temp = 1;//量程选择控制变量
void setup() {//初始化引脚
   lcd.begin(16, 2);
   pinMode(readhigh,INPUT);
   pinMode(readlow,INPUT);
   pinMode(inputpin,INPUT);
   Serial.begin(9600);//串口检查
}
void loop() {
  lcd.clear();//清屏控制数据显示位置
  int reading1 = analogRead(readhigh);
  int reading2 = analogRead(readlow);
  //读入电压模拟量值
  int reading = digitalRead(inputpin);
  //防抖动控制模块
if(reading != buttonstate)
  {
    long lastbuttontime = millis();
  }
    if( (millis()-lastbuttontime) > debouncetime)
    {
       if(reading != buttonstate)
      {
         reading = buttonstate;
         temp = -temp;
         delay(500);
      }
    }
 // 量程 V
    if(temp == 1)
    {
        double value1 = (reading1 - reading2) / 200.0;
        Serial.println("value1: ");
        Serial.println(value1);
        lcd.print(value1);
        lcd.setCursor(0,1);
        lcd.print("V");
    }
    //量程 mv
    else if(temp == -1)
    {
        if(reading1 >= reading2 )
        {
        double value2 = (reading1 - reading2)*4.9971+14.303;
        //线性规划模拟结果
        if((int)value2%10 == 4 && reading1!= 0)
        value2 -= 4.0;//修正系数
        if(value2 >= 15)
        {
        Serial.println("value2: ");
        Serial.println(value2);
        lcd.print(value2);
        lcd.setCursor(0,1);
        lcd.print("mV");
        }
        else//默认显示0.00mv
        {
        lcd.print(0.00);
        lcd.setCursor(0,1);
        lcd.print("mV");
        }
        }
        else
        {
        double value3 = (reading2 - reading1)*4.9971+14.303;
        if((int)value3%10 == 4 && reading2!= 0)
        value3 -= 4.0;
        if(value3 >= 15)
        {
        Serial.println("value3: ");
        Serial.println(value3);
        lcd.print("-");
        lcd.print(value3);
        lcd.setCursor(0,1);
        lcd.print("mV");
        }
        else
        {
        lcd.print(0.00);
        lcd.setCursor(0,1);
        lcd.print("mV");
        }
        }
    }
    delay(100);
}
